import {md5} from 'js-md5';

// 去除对象的null或者undefined属性
export const clearObject = (obj) => {
  for (const key in obj) {
    if(obj[key] === undefined || obj[key] === null) {
      delete obj[key];
    }
  }
  return obj
}

// 获取32位Md5
export const getMd5 = (str) => {
  return md5(str);
}

// 验证邮箱是否合法
export const validateEmail = (email) => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};

// 找好返回用户名和邮箱
export const explainAccount = (account) => {
  let isEmail = validateEmail(account);
  let username;
  let email;
  isEmail ? email = account : username = account;
  return {username, email};
}

// 验证密码强度 最少需要数字和大小写字母同时存在
export const validatePassword = (password) => {
  // 正则表达式，匹配包含数字和大小写字母的密码，最少长度为6位
  let regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/;
  if (regex.test(password)) {
    // console.log("密码强度符合要求");
    return true;
  } else {
    // console.log("密码强度不符合要求");
    return false;
  }
}

// 弹窗提示
export const elTip = (message, type = "success") => {
  ElMessage[type](message);
}

